<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="stylesheet" href="/static/mbyz/css/public.css">
    <link rel="stylesheet" href="/static/mbyz/css/login.css">
</head>
<body>
<div class="top"></div>
<div class="pad">
    <div class="back-name">
        <div class="text">手机号</div>
        <input type="number" id="phone" placeholder="请输入手机号"/>
    </div>
    <div class="back-code">
        <div class="code" type="button" id="getcode" onclick="getcode(this);">获取验证码</div>
        <input type="text" id="code" placeholder="请输入验证码">
    </div>
    <div id="subbmit">一键注册，立即抽奖</div>
</div>
<div class="bottom"></div>
</body>
<!--http://sasseur.pi-bc.com/mbyz/send_page-->
<script type='text/javascript' src='http://g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
<script type='text/javascript' src='/static/mbyz/js/mobile-detect.js' charset='utf-8'></script>
<script type='text/javascript' src='/static/mbyz/js/mui.js' charset='utf-8'></script>
<script>
    var idPre = window.location.href.split("?")[1];
    var r = idPre.split("=")[1];
    var i = idPre.split("&")[1];
    var num = i.split("=")[1];
    var room = r.split("&")[0];
    //获取验证码
    var code;       //获取到的验证码
    var t; //倒计时对象
    function getcode(e) {
        var phone = $("#phone").val();
        console.log(phone);
        if (!phone) {
            alert("手机号码不能为空");
            return;
        } else {
            if (!(/^1[3456789]\d{9}$/.test(phone))) {
                alert("手机号码有误，请重填")
            } else {
                //这里写死了 应该调用获取验证码的接口
                $.ajax({
                    type: "GET",
                    url: 'http://sasseur.pi-bc.com/mbyz/send_msg',
                    data: {phone: phone},
                    success: function (data) {
                        var datas = JSON.parse(data);
                        console.log(datas)
                        if (datas.errcode === 0) {
                            var code = datas.code;
                            //按照指定的周期（以毫秒计）来调用函数或计算表达式。
                            //最好是在ajax请求之后再调用函数
                            t = setInterval(function () {
                                countdown(e)
                            }, 1000);
                            // alert("获取验证码成功");
                            //获取验证码成功后调用倒计时函数
                            countdown(e);
                        } else {
                            alert(datas.errmsg);
                        }
                    }
                });
            }
        }
    }

    var time = 60;

    function countdown(e) {
        if (time == 0) {
            //这里时设置当时间到0的时候重新设置点击事件，并且默认time修改为60
            e.setAttribute("onclick", "getcode(this)");
            document.getElementById("getcode").innerText = "获取验证码";
            time = 60;
            clearInterval(t);
        } else {
            //这里是显示时间倒计时的时候点击不生效
            e.setAttribute("onclick", '');
            document.getElementById("getcode").innerHTML = "重新发送" + time;
            time--;
        }
    }

    //提交信息
    $('#subbmit').click(function (e) {
        var code = $("#code").val();
        var phone = $("#phone").val();
        if (!code) {
            alert("验证码不能为空");
            return;
        }
        $.ajax({
            type: "GET",
            url: 'http://sasseur.pi-bc.com/mbyz/check_msg',
            data: {phone: phone, code: code},
            success: function (data) {
                var submit_data = JSON.parse(data);
                console.log(submit_data, '92');
                if (submit_data.errcode === 200) {
                    console.log('95');
                    this.login()
                } else {
                    alert(submit_data.errmsg);
                }
            }
        })
    });

    //注册
    function login() {
        var phone = $("#phone").val();
        //判断数组中是否包含某字符串
        Array.prototype.contains = function (needle) {
            for (i in this) {
                if (this[i].indexOf(needle) > 0)
                    return i;
            }
            return -1;
        };
        var device_type = navigator.userAgent;//获取userAgent信息
        console.log(device_type, '85');
        var md = new MobileDetect(device_type);//初始化mobile-detect
        console.log(md.version('Webkit'), '145');         // 534.3
        var device_id = md.version('Webkit');
        var os = md.os();//获取系统
        var model = "";
        if (os == "iOS") {
            os = md.os() + md.version("iPhone");
            model = md.mobile();
            console.log(model, '150')
        } else {
            console.log('130')
            os = md.os() + md.version("Android");
            var sss = device_type.split(";");
            var i = sss.contains("Build/");
            console.log(i);
            if (i > -1) {
                model = sss[i].substring(0, sss[i].indexOf("Build/"));
                console.log(model, '135')
            } else {
                console.log('136')
            }
        }
        $.ajax({
            type: "GET",
            url: 'http://sasseur.pi-bc.com/mbyz/register',
            data: {phone: phone},
            success: function (data) {
                var login_data = JSON.parse(data);
                if (login_data.errcode === 200) {
                    console.log(login_data, '140');
                    // 跳转h5游戏
                    if (num == '' || room=='') {
                        console.log("num为空");
                        var n = 'n';
                        setTimeout(() => top.location.replace(`http://mob.pi-bc.com/v3/idea/JWZSQpSU?type=${n}`), 1000);
                    } else {
                        console.log("num不为空");
                        var yn = 'yn';
                        setTimeout(() => top.location.replace(`http://mob.pi-bc.com/v3/idea/JWZSQpSU?type=${yn}&room=${room}&num=${num}`), 1000);
                    }
                } else {
                    alert(login_data.errmsg);
                    window.location.href = "https://wxzhsc.sasseur.com/newlapp/#/menu/10047?go=user/index";

                }
            }
        })
    }

</script>
<script>

</script>
</html>